package com.zx.kitchen.index.common.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zx.kitchen.index.R;
import com.zx.kitchen.index.common.entity.CateListBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/3/5.
 */

public class CateListAdapter extends BaseAdapter {
    private List<CateListBean> objects = new ArrayList<CateListBean>();

    private Context context;
    private LayoutInflater layoutInflater;
    private int select = 0;

    public CateListAdapter(Context context, List<CateListBean> objects) {
        this.context = context;
        this.objects = objects;
        this.layoutInflater = LayoutInflater.from(context);
    }

    public void setSelect(int select) {
        this.select = select;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public CateListBean getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.adapter_catelist, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews((CateListBean) getItem(position), (ViewHolder) convertView.getTag(), position);
        return convertView;
    }

    private void initializeViews(CateListBean object, ViewHolder holder, int position) {
        holder.catelistName.setText(object.getName());
        holder.isnotCheck();
        if (select == position) {
            holder.check();
        }
    }

    protected class ViewHolder {
        private LinearLayout llCatelist;
        private TextView catelistName;

        public ViewHolder(View view) {
            catelistName = (TextView) view.findViewById(R.id.catelist_name);
            llCatelist = (LinearLayout) view.findViewById(R.id.ll_catelist);
        }

        public void check() {
            llCatelist.setBackgroundColor(context.getResources().getColor(R.color.blue));
            catelistName.setTextColor(context.getResources().getColor(R.color.white));
        }

        public void isnotCheck() {
            llCatelist.setBackgroundColor(context.getResources().getColor(R.color.white));
            catelistName.setTextColor(context.getResources().getColor(R.color.black));
        }
    }
}
